import Cookies from 'js-cookie'

const TokenKey = 'SESSION-Token'
const TokenKeyExpire = 'Session-WSCS-Token-expire'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token,isRememberMe) {
  let hour;
  if(isRememberMe){
    hour = 24 * 15;
  }else{
    hour = -1;
  }
  setCookie(TokenKey,token,hour);
  setCookie(TokenKeyExpire,hour,hour);
}

/**
 * 刷新cookie过期时间
 */
export function refreshSession(){
  let token = getToken(TokenKey);
  let timeOut = Cookies.get(TokenKeyExpire);
  if(token && timeOut > 0){
    setCookie(TokenKey,token,timeOut);
    setCookie(TokenKeyExpire,timeOut,timeOut);
  }
}

/**
 * 设置cookie
 * @param key
 * @param value
 * @param timeOut 过期时间，小时
 * @returns {*}
 */
export function setCookie(key,value,timeOut){
  if(timeOut > 0){
    let expiresDate = new Date(new Date().getTime() + timeOut * 60 * 60 * 1000) ;
    return Cookies.set(key, value,{expires: expiresDate})
  }else{
    return Cookies.set(key, value)
  }

}

/**
 * 删除
 * @returns {*}
 */
export function removeToken() {
  Cookies.remove(TokenKeyExpire)
  return Cookies.remove(TokenKey)
}
